<template>
  <a-modal v-model:open="open" title="可用科室" @ok="handleOk">
    <a-checkbox-group v-model="chooseList" :options="list" />
  </a-modal>
</template>

<script>
  import {
    keshiUse,
    keshiUpdate
  } from '@/api/medicine'
  import {
    Modal,
    message
  } from 'ant-design-vue';
  export default {
    data() {
      return {
        open: false,
        chooseList: [],
        list: [],
        allList: [],
        id: 0
      }
    },
    methods: {
      initData(id) {
        this.id = id
        keshiUse({
          medicineid: id
        }).then(res => {
          this.allList = res.data
          this.open = true
          let xuanzhong = []
          let list = res.data.map(resp => {
            if (resp.xuanzhong == 2) {
              xuanzhong.push(resp.name)
            }
            return resp.name
          })
          this.list = list
          this.chooseList = xuanzhong
        })
      },
      handleOk() {
        console.log(this.chooseList)
        let list = []
        this.chooseList.forEach(res => {
          this.allList.forEach(resp => {
            if (res == resp.name) {
              list.push(resp.id * 1)
            }
          })
        })
        console.log(list)
        keshiUpdate({
          medicineid: this.id,
          keshiids: list.join(',')
        }).then(res => {
          message.info('修改成功');
          this.open = false
        })
      }
    }
  }
</script>

<style>
</style>